package com.inossem.warehousestorage_inossem.adapter.query;

import android.app.Dialog;
import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.inossem.utils.base.Utils;
import com.inossem.utils.listener.NoDoubleClickListener;
import com.inossem.warehousestorage_inossem.R;
import com.inossem.warehousestorage_inossem.activity.query.QueryMaterielDetailActivity;
import com.inossem.warehousestorage_inossem.bean.query.QueryMaterielDetailBean;
import com.inossem.warehousestorage_inossem.dialog.DialogUtils;
import com.inossem.warehousestorage_inossem.utils.QRCodeEditTextWatcher;
import com.zhy.android.percent.support.PercentRelativeLayout;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by leij on 2017/6/6.
 */

public class QueryMaterielDetailAdapter extends BaseAdapter {

    private Context mContext;
    private List<QueryMaterielDetailBean.DataBean.BatchListBean.PositionListBean> mPolistionList;
    private List<QueryMaterielDetailBean.DataBean.BatchListBean> mBatchList;
    private LayoutInflater inflater;
    private QueryMaterielDetailActivity.QueryMaterielDetailListener mQueryMaterielDetailListener;
    private QueryMaterielDetailBean.DataBean mBean;
    private String mCondition, mMaterielCode, mMaterielDescription, mBatchNumber, mUnit, mInventoryNumber, mContract, mSupplier, mDemandDepartment, mPurchaseOrder, mInstorageDate, mSpecialInventoryFlag, mSpecialInventoryCode, mSpecialInventoryDescription, mType;

    public QueryMaterielDetailAdapter(Context context, QueryMaterielDetailBean.DataBean bean, QueryMaterielDetailBean.DataBean.BatchListBean batchItem, String condition, String type, QueryMaterielDetailActivity.QueryMaterielDetailListener queryMaterielDetailListener) {
        mContext = context;
        mBean = bean;
        mMaterielCode = mBean == null ? "" : mBean.getMaterielCode();
        mMaterielDescription = mBean == null ? "" : mBean.getMaterielDescription();
        mBatchList = mBean == null ? new ArrayList<>() : mBean.getBatchList();
        mBatchNumber = batchItem.getBatchNumber();
        mUnit = batchItem.getUnit();
        mInventoryNumber = batchItem.getInventoryNumber() == null ? "" : (batchItem.getInventoryNumber() + mUnit);
        mContract = batchItem.getContract();
        mSupplier = batchItem.getSupplier();
        mDemandDepartment = batchItem.getDemandDepartment();
        mPurchaseOrder = batchItem.getPurchaseOrder();
        mInstorageDate = batchItem.getInStorageDate();
        mSpecialInventoryFlag = batchItem.getSpecialInventoryFlag();
        mSpecialInventoryCode = batchItem.getSpecialInventoryCode();
        mSpecialInventoryDescription = batchItem.getSpecialInventoryDescription();
        mPolistionList = batchItem.getPositionList() == null ? new ArrayList<>() : batchItem.getPositionList();
        mQueryMaterielDetailListener = queryMaterielDetailListener;
        inflater = LayoutInflater.from(context);
        mCondition = condition;
        mType = type;
    }

    @Override
    public int getCount() {
        return mPolistionList.size() + 1;
    }

    @Override
    public Object getItem(int position) {
        return mPolistionList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (position == 0) {
            convertView = inflater.inflate(R.layout.view_query_materiel_list_first_item, null);
            TextView materielCode = (TextView) convertView.findViewById(R.id.materielCode);
            TextView materielDescription = (TextView) convertView.findViewById(R.id.materielDescription);
            TextView batchNumber = (TextView) convertView.findViewById(R.id.batchNumber);
            TextView inventoryNumber = (TextView) convertView.findViewById(R.id.inventoryNumber);
            TextView contract = (TextView) convertView.findViewById(R.id.contract);
            TextView supplier = (TextView) convertView.findViewById(R.id.supplier);
            TextView demandDepartment = (TextView) convertView.findViewById(R.id.demandDepartment);
            TextView purchaseOrder = (TextView) convertView.findViewById(R.id.purchaseOrder);
            TextView inStorageDate = (TextView) convertView.findViewById(R.id.inStorageDate);
            TextView specialInventoryFlag = (TextView) convertView.findViewById(R.id.specialInventoryFlag);
            TextView specialInventoryCode = (TextView) convertView.findViewById(R.id.specialInventoryCode);
            TextView specialInventoryDescription = (TextView) convertView.findViewById(R.id.specialInventoryDescription);
            PercentRelativeLayout conditionLayout = (PercentRelativeLayout) convertView.findViewById(R.id.conditionLayout);
            PercentRelativeLayout materielInfoLayout = (PercentRelativeLayout) convertView.findViewById(R.id.materielInfoLayout);
            EditText condition = (EditText) convertView.findViewById(R.id.condition);
            ImageView search = (ImageView) convertView.findViewById(R.id.search);
            if (QueryMaterielDetailActivity.BATCH.equals(mType)) {
                conditionLayout.setVisibility(View.GONE);
            } else {
                conditionLayout.setVisibility(View.VISIBLE);
                condition.setText(mCondition);
                condition.addTextChangedListener(new QRCodeEditTextWatcher(condition) {
                    @Override
                    public void onScan(String text) {
                        Utils.closeSoftKeyboard(mContext,condition);
                        mQueryMaterielDetailListener.onNoDoubleClickListener(text);
                    }
                });
                search.setOnClickListener(new NoDoubleClickListener() {
                    @Override
                    public void onNoDoubleClick(View v) {
                        if (TextUtils.isEmpty(condition.getText().toString())) {
                            Utils.middleToast(mContext, "物料编码不可以为空");
                        } else {
                            Utils.closeSoftKeyboard(mContext,condition);
                            mQueryMaterielDetailListener.onNoDoubleClickListener(condition.getText().toString());
                        }
                    }
                });
            }
            if (mBean == null) {
                materielInfoLayout.setVisibility(View.GONE);
            } else {
                materielInfoLayout.setVisibility(View.VISIBLE);
                materielCode.setText(mMaterielCode);
                materielDescription.setText(mMaterielDescription);
                batchNumber.setText(mBatchNumber);
                inventoryNumber.setText(mInventoryNumber);
                contract.setText(mContract);
                supplier.setText(mSupplier);
                demandDepartment.setText(mDemandDepartment);
                purchaseOrder.setText(mPurchaseOrder);
                inStorageDate.setText(mInstorageDate);
                specialInventoryFlag.setText(mSpecialInventoryFlag);
                specialInventoryCode.setText(mSpecialInventoryCode);
                specialInventoryDescription.setText(mSpecialInventoryDescription);
                if (mBatchList.size() > 1) {
                    batchNumber.setTextColor(ContextCompat.getColor(mContext, R.color.activity_receive_delivery_overview_font_color));
                    batchNumber.setOnClickListener(new NoDoubleClickListener() {
                        @Override
                        public void onNoDoubleClick(View v) {
                            DialogUtils.getBatchNumberDialog(mContext, mBatchList, new DialogUtils.BatchNumberListener() {
                                @Override
                                public void onSelect(Dialog dialog, QueryMaterielDetailBean.DataBean.BatchListBean batchItem) {
                                    mQueryMaterielDetailListener.onChangeBatch(batchItem);
                                }
                            }).show();
                        }
                    });
                } else {
                    batchNumber.setTextColor(ContextCompat.getColor(mContext, R.color.common_font_content));
                    batchNumber.setOnClickListener(null);
                }
            }

        } else {
            QueryMaterielDetailBean.DataBean.BatchListBean.PositionListBean positionItem = mPolistionList.get(position - 1);
            ViewHolder viewHolder;
            if (convertView == null || (convertView != null && convertView.getTag() == null)) {
                convertView = inflater.inflate(R.layout.view_query_materiel_list, null);
                viewHolder = new ViewHolder();
                //初始化控件
                initView(viewHolder, convertView);
                convertView.setTag(viewHolder);
            } else {
                viewHolder = (ViewHolder) convertView.getTag();
            }
            //初始化数据
            initData(mContext, viewHolder, convertView, positionItem);
        }
        return convertView;
    }

    private void initView(ViewHolder viewHolder, View view) {
        viewHolder.positionNumber = (TextView) view.findViewById(R.id.positionNumber);
        viewHolder.positionInventoryNumber = (TextView) view.findViewById(R.id.positionInventoryNumber);
    }

    private void initData(Context context, ViewHolder viewHolder, View view, QueryMaterielDetailBean.DataBean.BatchListBean.PositionListBean positionItem) {
        viewHolder.positionNumber.setText(positionItem.getPositionNumber());
        viewHolder.positionInventoryNumber.setText(positionItem.getPositionInventoryNumber() + mUnit);
    }

    private class ViewHolder {
        private TextView positionNumber, positionInventoryNumber;
    }
}
